{extend name="platform/new_base" /}
{block name="resource"}
<style>
    .label-danger a{padding-top:5px;}
</style>
{/block}
{block name="main"}
            <!-- page -->
            <div class="mb-20">
                <a href="{:__URL('PLATFORM_MAIN/Goods/addGoodsCategory')}" class="btn btn-primary"><i class="icon icon-add1"></i> 添加分类</a>
            </div>
            <table class="table v-table table-auto-center tree" >
                <thead>
                <tr>
                    <th class="text-left"><i class="treegrid-expander icon icon-minus toggle_tree"></i></th>
                    <th>排序</th>
                    <th>商品分类</th>
                    <th>关联品类</th>
                    <th>是否显示</th>
                    <th  class="col-md-2 pr-14 operationLeft">操作</th>
                </tr>
                </thead>
                <tbody >
                    {if $category_list}
                {volist name="category_list" id="v"}
                <tr class="treegrid-{$v.category_id}">
                    <td class="text-left"></td>
                    <td>
                        <input type="text" data-category_id="{$v.category_id}" class="form-control sort-form-control" value="{$v.sort}">
                    </td>
                    <td>
                        <div><input type="text" data-category_id="{$v.category_id}" class="form-control change_category_name" value="{$v.category_name}"></div>
                    </td>
                    
                    <td>
                        {$v.attr_name}
                    </td>
                    <td>
                        {if condition="$v['is_visible'] eq '1' "}
                        <a href="javascript:void(0);" class="label label-success is_show" data-is_show="0">是</a>
                        {else}
                        <a href="javascript:void(0);" class="label label-danger is_show" data-is_show="1">否</a>
                        {/if}
                        <input type="hidden" name="category_id" value="{$v.category_id}">
                        <input type="hidden" name="test" value="">
                    </td>
                    <td class="operationLeft fs-0">
                        <input type="hidden" name="category_id" value="{$v.category_id}">
                        <a href="{:__URL('PLATFORM_MAIN/Goods/addGoodsCategory?category_id='.$v['category_id'])}" class="btn-operation">添加子分类</a>
                        <a href="{:__URL('PLATFORM_MAIN/goods/updategoodscategory?category_id='.$v['category_id'])}" class="btn-operation">编辑</a>
                        <a href="javascript:void(0);" class="btn-operation delete_category text-red1">删除</a>
                        
                    </td>
                </tr>

                <!--二级菜单-->
                {if condition="$v['child_list'] neq '' and $v['is_parent'] eq '1' "}
                {volist name="v['child_list']" id="two"}
                <tr class="treegrid-{$two.category_id} treegrid-parent-{$two.pid}">
                    <td class="text-left"></td>
                    <td>
                        <input type="text" data-category_id="{$two.category_id}" class="form-control sort-form-control" value="{$two.sort}">
                    </td>
                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">|——</span>
                            <input type="text" data-category_id="{$two.category_id}" class="form-control change_category_name" value="{$two.category_name}">
                        </div>
                    </td>
                    
                    <td>
                        {$two.attr_name}
                    </td>
                    <td>
                        {if condition="$two['is_visible'] eq '1' "}
                        <a href="javascript:void(0);" class="label label-success is_show" data-is_show="0">是</a>
                        {else}
                        <a href="javascript:void(0);" class="label label-danger is_show" data-is_show="1">否</a>
                        {/if}
                        <input type="hidden" name="category_id" value="{$two.category_id}">
                    </td>
                    <td class="operationLeft fs-0">
                        <input type="hidden" name="category_id" value="{$two.category_id}">
                        <a href="{:__URL('PLATFORM_MAIN/Goods/addGoodsCategory?category_id='.$two['category_id'])}" class="btn-operation">添加子分类</a>
                        <a href="{:__URL('PLATFORM_MAIN/goods/updategoodscategory?category_id='.$two['category_id'])}" class="btn-operation">编辑</a>
                        <a href="javascript:void(0);" class="btn-operation delete_category text-red1">删除</a>
                        
                    </td>
                </tr>

                <!--三级菜单-->
                {if condition="$two['child_list'] neq '' "}
                {volist name="two['child_list']" id="three"}
                <tr class="treegrid-{$three.category_id} treegrid-parent-{$three.pid}">
                    <td class="text-left"></td>
                    <td>
                        <input type="text" data-category_id="{$three.category_id}" class="form-control sort-form-control" value="{$three.sort}">
                    </td>
                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">|—————</span>
                            <input type="text" data-category_id="{$three.category_id}" class="form-control change_category_name" value="{$three.category_name}">
                        </div>
                        <div class="pl-30"></div>
                    </td>
                    <td>
                        {$three.attr_name}
                    </td>
                    <td>
                        {if condition="$three['is_visible'] eq '1' "}
                        <a href="javascript:void(0);" class="label label-success is_show" data-is_show="0">是</a>
                        {else}
                        <a href="javascript:void(0);" class="label label-danger is_show" data-is_show="1">否</a>
                        {/if}
                        <input type="hidden" name="category_id" value="{$three.category_id}">
                    </td>
                    <td class="operationLeft fs-0">
                        <input type="hidden" name="category_id" value="{$three.category_id}">
                        <a href="{:__URL('PLATFORM_MAIN/goods/updategoodscategory?category_id='.$three['category_id'])}" class="btn-operation">编辑</a>
                        <a href="javascript:void(0);" class="btn-operation delete_category text-red1">删除</a>
                        
                    </td>
                </tr>
                {/volist}
                {/if}
                {/volist}
                {/if}
                <!--二级分类，无三级菜单-->
                {if condition="$v['is_parent'] neq '1' "}
                {volist name="v['child_list']" id="two"}
                <tr class="treegrid-{$two.category_id} treegrid-parent-{$two.pid}">
                    <td class="text-left"></td>
                    <td>
                        <input type="text" data-category_id="{$two.category_id}" class="form-control sort-form-control" value="{$two.sort}">
                    </td>
                    <td>
                        <div class="pl-15"><input type="text" data-category_id="{$two.category_id}" class="form-control change_category_name" value="{$two.category_name}"></div>
                    </td>
                    <td>
                        {$two.attr_name}
                    </td>
                    <td>
                        {if condition="$two['is_visible'] eq '1' "}
                        <a href="javascript:void(0);" class="label label-success is_show" data-is_show="0">是</a>
                        {else}
                        <a href="javascript:void(0);" class="label label-danger is_show" data-is_show="1">否</a>
                        <input type="hidden" name="category_id" value="{$two.category_id}">
                        {/if}
                    </td>
                    <td class="operationLeft fs-0">
                        <input type="hidden" name="category_id" value="{$two.category_id}">
                        <a href="javascript:void(0);" class="btn-operation">编辑</a>
                        <a href="javascript:void(0);" class="btn-operation delete_category text-red1">删除</a>
                    </td>
                </tr>
                {/volist}
                {/if}
                {/volist}
                {else}
                <tr align="center"><td colspan="6">暂无符合条件的数据记录</td></tr>
                {/if}
                </tbody>
            </table>

            <!-- page end -->
{/block}

{block name="script"}
<script>
    require(['util'],function(util){
        util.treegrid('.tree');
        $(".toggle_tree").toggle(function(){
            $(".tree").treegrid("collapseAll");
            $(this).removeClass("icon-minus").addClass("icon-plus");
        },function(){
            $(".tree").treegrid("expandAll");
            $(this).removeClass("icon-plus").addClass("icon-minus");
        })
        util.tips();

        //删除分类
        $('.delete_category').on('click', function () {
            var category_id = $(this).siblings("input[name='category_id']").val();
            util.alert('确认删除此分类吗 ？', function () {
                $.ajax({
                    type : "post",
                    url : "{:__URL('PLATFORM_MAIN/Goods/deleteGoodsCategory')}",
                    async : true,
                    data : {
                        "category_id" : category_id,
                    },
                    success : function(data) {
                        if (data["code"] > 0) {
                            util.message(data["message"],'success',"{:__URL('PLATFORM_MAIN/Goods/goodscategorylist')}");
                        }else{
                            util.message(data["message"],'danger');
                        }
                    }
                })
            })
        })
        //排序
        $('.sort-form-control').change(function(){
            var category_id = $(this).data('category_id');
            var sort_val = $(this).val();
            // console.log(category_id);return;
            $.ajax({
                type : "post",
                url : "{:__URL('PLATFORM_MAIN/Goods/changeGoodsCategorySort')}",
                async : true,
                data : {category_id : category_id, sort_val : sort_val},
                success : function(data) {
                    // console.log(data);return;
                    if (data["code"] > 0) {
                        util.message(data["message"],'success',"{:__URL('PLATFORM_MAIN/Goods/goodscategorylist')}");
                    }else{
                        util.message(data["message"],'danger');
                    }
                }
            })
        })
        //修改分类名
        $('.change_category_name').change(function(){
            var category_id = $(this).data('category_id');
            var category_name = $(this).val();
            $.ajax({
                type : "post",
                url : "{:__URL('PLATFORM_MAIN/Goods/changeGoodsCategoryName')}",
                async : true,
                data : {category_id : category_id, category_name : category_name},
                success : function(data) {
                    if (data["code"] > 0) {
                        util.message(data["message"],'success',"{:__URL('PLATFORM_MAIN/Goods/goodscategorylist')}");
                    }else{
                        util.message(data["message"],'danger');
                    }
                }
            })
        })

       
        //是否显示
        $('.is_show').click(function(){
            test = 'test';
            $(this).removeClass('is_show');
            var status = $(this).data('is_show');
            is_show(this, status);
        })
        function is_show(obj, status){
            var category_id = $(obj).next().val();
            //0不显示
            if(status == 0){
                var is_visible = 0;
                $msg = '是否更改为不显示？';
            }else{
                var is_visible = 1;
                $msg = '是否更改为显示？';
            }
            util.alert($msg,function(){
                $.ajax({
                    type : "post",
                    url : "{:__URL('PLATFORM_MAIN/Goods/changeGoodsCategoryShow')}",
                    async : true,
                    data : {category_id : category_id, is_visible : is_visible},
                    success : function(data) {
                        if (data["code"] > 0) {
                            util.message(data["message"],'success',"{:__URL('PLATFORM_MAIN/Goods/goodscategorylist')}");
                        }else{
                            util.message(data["message"],'danger');
                        }
                        $(this).addClass('is_show');
                    }
                })
            })

        }
    })
</script>
{/block}